﻿using System.Web;
using System.Web.Mvc;
using System.IO;
using Web.Model;
using NHibernate;
using System.Drawing;

namespace Web.HelperClasses.AvatarService
{
    public class ProjectAvatarService : AvatarService
    {
        public static string NoAvatarSmall { get; private set; }
		public static string NoAvatarLarge { get; private set; }

        static ProjectAvatarService()
        {
			NoAvatarSmall = VirtualPathUtility.ToAbsolute("~/Content/img/no_project_avatar_small.jpg");
			NoAvatarLarge = VirtualPathUtility.ToAbsolute("~/Content/img/no_project_avatar_large.jpg");
		}

        [HttpPost]
        public override void UploadNew(int Id, Stream avatarStream)
        {
            var project = session.Get<Project>(Id);
            var avatarImg = new Bitmap(avatarStream);

            var smallSized = ResizeImage(avatarImg, width: 64);
            var largeSized = ResizeImage(avatarImg, width: 192);

            string smallEditUrl = yandexFotki.GetOrCreateAlbum("Small avatars");
            string largeEditUrl = yandexFotki.GetOrCreateAlbum("Large avatars");
            var small = yandexFotki.UploadPhotoWithName(smallSized, smallEditUrl, project.AlbumName);
            var large = yandexFotki.UploadPhotoWithName(largeSized, largeEditUrl, project.AlbumName);

            if (project.ProjectAvatar != null)
                RemoveAvatar(project.ProjectAvatar);
            project.ProjectAvatar = new Avatar
            {
                Small = small,
                Large = large
            };
        }

        public ProjectAvatarService(ISession session, YandexFotki yandexFotki)
            :base(yandexFotki)
        {
            this.session = session;
        }

        private readonly ISession session;
    }
}